package com.wswy.wzcx.ui.component;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.data.WZDBMode;
import com.wswy.wzcx.utils.StringUtils;

/**
 * Created by wangduheng26 on 2018/3/22.
 * wangduheng26@gmail.com
 */

public class WZDBChooseComponent extends DataBindingComponent<WZDBMode> {
    private TextView label1, label2, label3;
    private TextView tvAddress, tvReason, tvTime,tvTip;
    private ImageView ivBox;
    private OnBoxListener listener;

    public static Drawable createDrawable(GradientDrawable drawable, GradientDrawable disableDrawable) {
        StateListDrawable res = new StateListDrawable();
        res.addState(new int[]{android.R.attr.state_enabled}, drawable);
        res.addState(new int[]{}, disableDrawable);
        return res;
    }
    public void changeStatus(boolean contains){
        ivBox.setSelected(contains);
    }
    public WZDBChooseComponent(ViewGroup parent) {
        super(parent, R.layout.wzdb_choose_item_layout);
        View view = getView();
        ivBox = view.findViewById(R.id.iv_box);
        label1 = view.findViewById(R.id.label1);
        label2 = view.findViewById(R.id.label2);
        label3 = view.findViewById(R.id.label3);
        tvAddress = view.findViewById(R.id.tv_address);
        tvReason = view.findViewById(R.id.tv_reason);
        tvTime = view.findViewById(R.id.tv_time);
        tvTip=view.findViewById(R.id.tv_tip);
        // prepare
        ivBox.setSelected(false);
        label1.setLineSpacing(1f, 1.1f);
        label2.setLineSpacing(1f, 1.1f);
        label3.setLineSpacing(1f, 1.1f);

        int roundRadius = 15; // 8px not dp

        GradientDrawable disableDrawable = new GradientDrawable();
        disableDrawable.setColor(ContextCompat.getColor(view.getContext(),R.color.textColorPrimary_20));
        disableDrawable.setCornerRadius(roundRadius);

        GradientDrawable drawable1 = new GradientDrawable();
        drawable1.setColor(ContextCompat.getColor(view.getContext(),R.color.accentColor4));
        drawable1.setCornerRadius(roundRadius);

        label1.setBackground(createDrawable(drawable1, disableDrawable));

        GradientDrawable drawable2 = new GradientDrawable();
        drawable2.setColor(ContextCompat.getColor(view.getContext(),R.color.colorPrimary));
        drawable2.setCornerRadius(roundRadius);
        label2.setBackground(createDrawable(drawable2, disableDrawable));

        GradientDrawable drawable3 = new GradientDrawable();
        drawable3.setColor(ContextCompat.getColor(view.getContext(),R.color.accentColor1));
        drawable3.setCornerRadius(roundRadius);
        label3.setBackground(createDrawable(drawable3, disableDrawable));

    }

    @Override
    public void bind(WZDBMode data) {
        super.bind(data);
//        if (listener != null) {
//            ivBox.setOnClickListener(v -> listener.onClick(v, getData()));
//        }
        ivBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getView().performClick();
            }
        });
        Context context = label1.getContext();
//        ivBox.setVisibility(data.getNopass()?View.INVISIBLE:View.VISIBLE);
        boolean isPass = !data.getNopass();
        isPass = data.checkDJ();
        if(!isPass){
            tvTip.setVisibility(View.VISIBLE);
            tvTip.setText(data.getTip());
            tvTip.setTextColor(ContextCompat.getColor(context,data.getTipColor()));
        }else{
            tvTip.setVisibility(View.GONE);
        }
        label1.setEnabled(isPass);
        label2.setEnabled(isPass);
        label3.setEnabled(isPass);
        tvAddress.setEnabled(isPass);
        label1.setText(createSpann(context, StringUtils.formatMoney(data.getFaKuanJinHe()),R.string.label1_params));

        label2.setText(createSpann(context,String.valueOf(data.getDeductPoint()),R.string.label2_params));

        label3.setText(createSpann(context,StringUtils.formatMoney(data.getDaiBanFei()),R.string.label3_params));

        tvAddress.setText(data.getWeiFaDiZhi());
        tvReason.setText(data.getWeiFaXinWeiMingCheng());
        tvTime.setText(data.getWeiFaSheJiang());

    }

    public interface OnBoxListener {
        void onClick(View v, WZDBMode info);
    }

    public void setBoxClickListener(OnBoxListener l) {
        listener = l;
    }

    public SpannableString createSpann(Context context, String value, @StringRes int strId){
        SpannableString msp3 = new SpannableString(context.getString(strId, value));
        msp3.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp3;
    }
}
